<?php 
	 require_once('header_sidebar.php');
?>
	<aside class="right-side">
	    <!-- Content Header (Page header) -->
	    <section class="content-header">
	        <h1>
	            <?php echo $translate->translate('Calibration'); ?>
	        </h1>
	        <ol class="breadcrumb">
	            <li><i class="fa fa-dashboard"></i> <?php echo $translate->translate('HomePageRating'); ?></li>
	            <li class="active"><?php echo $translate->translate('Calibration'); ?></li>
	        </ol>
	    </section>
	    <section class="content">
	    	<?php if (isset($_GET['id'])) : ?>
	    		<div class="alert alert-success alert-dismissable">
                    <i class="fa fa-check"></i>
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <?php echo $translate->translate('successCalibration') . '<b>' . $_GET['id'] . '</b>'; ?>
                </div>
	    	<?php endif ?>
	    	<div class="box box-primary">
                <div class="box-header">
                    <h3 class="box-title"><?php echo $translate->translate('DescriptionCalibration'); ?></h3>
                </div>
                <!-- form start -->
                <form role="form" action="" method="post">
                    <div class="box-body">
                        <div id="toCopy" class="form-group" style="width:10%">
                            <label><?php echo $translate->translate('NRating'); ?> :</label>
                             <div class="input-group">
					            <div class="input-group-addon">
					                <i class="fa fa-tag"></i>
					            </div>
					            <input id="id_0" name="id_0" type="text" class="form-control" value="">
					        </div>
                        </div>
                        <div class="form-group">
                            <label><?php echo $translate->translate('NRating'); ?> :</label>
                             <div class="input-group" style="width:10%">
					            <div class="input-group-addon">
					                <i class="fa fa-tag"></i>
					            </div>
					            <input id="id_1" name="id_1" type="text" class="form-control">
					        </div>
                        </div>
                        <div id="addField" >
                            <i class="fa fa-fw fa-plus-square-o" style="cursor: pointer;"></i><i style="cursor: pointer;" onclick="addField();"><?php echo $translate->translate('addField'); ?></i>
                        </div>
                    </div><!-- /.box-body -->
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary">Go !</button>
                    </div>
                </form>
		    </div>
		    <?php if (isset($_POST)) : ?>
		    	<?php
		    		require_once('Class/rating.Class.php');
		    		require_once('Class/connexionDB.Class.php');
		    		require_once('Class/calibration.Class.php');

		    		$calibration 	= new calibration();
		    		$rating 		= new rating();
		    		$db 			= new connexionDB();

		    		$calibration->setValidArray($_POST);
		    		$result = $calibration->getRatingData();
		    	 ?>
		    	<?php if(!empty($result)) : ?>
		    		<?php 
		    			$maxCol = count($result, COUNT_NORMAL);
		    			if ($maxCol < 12)
		    			{
		    				$maxCol = 12 / $maxCol;
		    			}
		    			else
		    			{
		    				$maxCol = 1;
		    			}
	    			?>
	    			<?php 
	    				$nbRating = 0;
	    			?>
		    		<?php foreach ($result as $value) : ?>
					    <div class="<?php echo 'col-md-' . $maxCol;?>">
					    	<div class="box">
							    <div class="box-body">
							    	<h4>
							    		#<?php echo $value['id']; ?> - <?php echo $translate->translate('titleCalibration'); ?>
							    		<?php echo $value['name_consultant']; ?>  -  <b style="color:green;"><?php echo $value['final_mark'] ?>/100</b> 
							    		<small>
							    			<i>
							    				le <?php echo $value['date_eval']; ?>
								    			par <?php echo $value['name_rater'] ?>
								    		</i>
								    	</small>
							    	</h4> <!-- Display first rating : -->
							    	<?php 
							    		$countryName = $db->getCountryName(intval($value['id_country']));
							    		
							    		echo '<b>' . $translate->translate('DateCall') . '</b> : <span id="date_call_'. $nbRating .'">' . $value['date_call'] . '</span><br>';
								    	echo '<b>' . $translate->translate('DateEvaluation') . '</b> : ' . $value['date_eval'] . '<br>';
								    	echo '<b>' . $translate->translate('NAkio') . '</b> : <span id="n_akio_'. $nbRating .'">' . $value['n_akio'] . '</span><br>';
								    	echo '<b>' . $translate->translate('NOrder') . '</b> : <span id="n_order_'. $nbRating .'">' . $value['n_order'] . '</span><br>';
								    	echo '<b>' . $translate->translate('Improvement') . '</b> : ' . $value['improvement'] . '<br>';
										echo '<b>' . $translate->translate('markWONullDetail') . '</b> : ' . $value['optional_mark'] . '<br>';
								    	echo '<b>' . $translate->translate('AdminNewAccountAccountCountry') . '</b> : <span id="country_name_'. $nbRating .'">' . $countryName['name'] . '</span><br>';
							    		echo '<b>' . $translate->translate('EvaluationType') . '</b> : ' . ucfirst($value['eval_type']) . '<br>';
							    		echo '<b>' . $translate->translate('AdminNewAccountAccountGroup') . '</b> : <span id="groups_'. $nbRating .'">' . $value['groups'] . '</span><br>';
							    		echo '<b>' . $translate->translate('team') . '</b> : ' . $value['team'] . '<br>';

							    		if (intval($value['id_recipient']) != 0)
						    			{
						    				$recipientName = $db->getRecipientName(intval($value['id_recipient']));
						    				echo '<b>' . $translate->translate('Recipient') . '</b> : ' . $recipientName['name'] . '<br>';
						    			}

						    			if (intval($value['id_topic']) != 0)
						    			{
						    				$topicName = $db->getTopicName($value['id_topic']);
						    				echo '<b>' . $translate->translate('NameTopic') . '</b> : ' . $topicName['description'] . '<br>';
						    			}
							    	?>
							    </div>				    
							</div>
						</div>
						<?php  $nbRating++; ?>
					<?php endforeach ?>
					<div id="nb_rating" hidden value="<?php echo $nbRating; ?>"></div>
					<div class="col-md-12">
						<?php 
							$valid = $calibration->isSameEvaluation($result);
						?>
						<?php if (!$valid) : ?>
							<div class="alert alert-danger alert-dismissable">
                                <i class="fa fa-ban"></i>
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                                <b><?php echo $translate->translate('noCalibration'); ?> ! </b><?php echo $translate->translate('descriptionErrorCalibration'); ?>
                            </div>
						<?php endif ?>
						<?php if ($valid) : ?>
							<div class="box">
								<div class="box-body">
									<div class="box-title">
										<h3><?php echo $translate->translate('Criteria'); ?> : </h3>
									</div>
									<table class="table table-condensed">
				                        <tbody>
				                            <tr>
				                                <th></th>
				                            <?php
				                            	foreach ($result as $value) {
				                            		$criteria[] = $db->getCriteriaDescription($value['id']);

				                            		$rating_ids[] = $value['id'];
				                            		echo '<th>'. $value['name_rater'] .' - #'. $value['id'] .' - '. $value['final_mark'] .'/100<a style="margin-left:10px;" target="_blank" class="fa fa-pencil" href="show_rating.php?id='. $value['id'] .'"></a></th>';
				                            	}
				                            	echo '</tr>';
				                            	$criteriaName = $calibration->getCriteriaName($criteria[0]);
				                            	foreach ($criteriaName as $key => $name)
				                            	{
				                            		echo 	'<tr><th>' . $name['name'] . '</th>';
				                            		
				                            		foreach ($rating_ids as $rating_id)
				                            		{
														$criteria = $db->getCriteriaDescription($rating_id);
														foreach ($criteria as $crit)
														{
															if ($crit['original_id'] == $name['id_criteria'])
															{
																echo '<td>' . $crit['mark'] . ' ( ' . $crit['name_mark'] . ' )</td>';
															}
														}
														
													}
					                            	echo '</tr>';
					                            }
											?>
										</tbody>
				                    </table>
				                    <div class="box-footer">
				                    	<form id="saveForm" action="save_calibration.php" method="post">
				                    		<input hidden type="text" id="id_account" 		name="id_account" value="<?php echo $session->id_account;?>">
				                    		<input hidden type="text" id="n_akio_form" 		name="n_akio_form" 		>
				                    		<input hidden type="text" id="n_order_form"		name="n_order_form"		>
				                    		<input hidden type="text" id="groups_form"		name="groups_form"		>
				                    		<input hidden type="text" id="id_country_form"	name="id_country_form"	>
				                    		<input hidden type="text" id="date_call_form"	name="date_call_form"	>
				                    		<?php foreach ($rating_ids as $value) : ?>
				                    			<input hidden type="text" name="rating_<?php echo $value; ?>" value="<?php echo $value; ?>">
				                    		<?php endforeach ?>
				                    		<div onclick="actualiser();" type="submit" class="btn btn-success" style="margin-left:10px;"><i class="fa fa-fw fa-refresh"></i></div>
					                        <div onclick="isSameGroup();" class="btn btn-danger pull-right">Sauvegarder le calibrage</div>
					                    </form>
				                    </div>
								</div>
							</div>
						<?php endif ?>
					</div>
				<?php endif ?>
			<?php endif ?>
			<!-- Modal -->
			<div class="modal fade" id="error-message" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
				<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
						<h4 class="modal-title has-error" id="myModalLabel"><?php echo $translate->translate('error'); ?></h4>
					</div>
					<div class="modal-body">
						<div class="alert alert-danger alert-dismissable">
			                <i class="fa fa-ban"></i>
			                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
			            	<?php echo $translate->translate('errorCalibration'); ?>    
			            </div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					</div>
				</div>
				</div>
			</div>
	    </section>
	</aside>
<?php 
	 require_once('footer.php');
?>
<script>
	var index = 2;
	function addField()
	{
		var copiedDiv = $('#toCopy').clone();

		copiedDiv.find('#id_0').val('');
		copiedDiv.find('#id_0').attr('name', 'id_' + index);
		copiedDiv.find('#id_0').attr('id', 'id_' + index);

		index++;

		copiedDiv.insertBefore('#addField');
	}

	function actualiser()
	{
		location.reload(true);
	}
	
	function isSameValues(data)
	{
		if (data.length != 1 && typeof data != 'string')
		{
			return false;
		}
		return true;
	}

	function isSameGroup()
	{
		var nbRating 	= $('#nb_rating').attr('value');
		var nAkio 		= [];
		var NOrder 		= [];
		var groups 		= [];
		var countries 	= [];
		var datesCall 	= [];
		var error 		= 0;

		for (var i = nbRating - 1; i >= 0; i--)
		{
			akio 		= $('#n_akio_' + i).text();
			order 		= $('#n_order_'+ i).text();
			group 		= $('#groups_' + i).text();
			country 	= $('#country_name_' + i).text();
			dateCall 	= $('#date_call_' + i).text();

			if ($.inArray(akio, nAkio) == -1)
			{
				nAkio.push(akio);	
			}
			
			if ($.inArray(order, NOrder) == -1)
			{
				NOrder.push(order);	
			}
			
			if ($.inArray(group, groups) == -1)
			{
				groups.push(group);
			}

			if ($.inArray(country, countries) == -1)
			{
				countries.push(country);
			}

			if ($.inArray(dateCall, datesCall) == -1)
			{
				datesCall.push(dateCall);
			}	
		};

		if (!isSameValues(nAkio))
		{
			$("[id^='n_akio_']").attr('style', 'color:red');	
			error++;
		}

		if (!isSameValues(NOrder))
		{
			$("[id^='n_order_']").attr('style', 'color:red');	
			error++;
		}

		if (!isSameValues(groups))
		{
			$("[id^='groups_']").attr('style', 'color:red');	
			error++;
		}

		if (!isSameValues(countries))
		{
			$("[id^='country_name_']").attr('style', 'color:red');	
			error++;
		}

		if (!isSameValues(dateCall))
		{
			$("[id^='date_call_']").attr('style', 'color:red');	
			error++;
		}

		if (error != 0)
		{
			$('#error-message').modal('show');	
		}
		else
		{
			$('#n_akio_form').attr('value', nAkio);
			$('#n_order_form').attr('value', NOrder);
			$('#groups_form').attr('value', groups);
			$('#id_country_form').attr('value', countries);
			$('#date_call_form').attr('value', dateCall);
			$('#saveForm').submit();
		}
	}
</script>